%% -*- coding: utf-8 -*-

%% @doc templ_parse_handler のテスト
%%
-module(t_templ_parse_handler).
-import(templ_parse_handler, [init/0,
							  text/1,start_if/1,else/0,start_let/2,start_foreach/2,
							  end_stmt/0,end_if/0,end_let/0,end_foreach/0,get_stmt/0]).

-include_lib("eunit/include/eunit.hrl").

expr_test() ->
	init(),
	text("hoge"),
	start_if({expr, true}),
	text("foo"),
	else(),
	text("bar"),
	end_stmt(),
	?assertMatch([[{ok,"hoge"}, {'if',_}, [{ok,"foo"}], elseif, [{ok,"bar"}]]], get_stmt()).

expr1_test() ->
	init(),
	start_if({expr, true}),
	text("foo"),
	else(),
	text("bar"),
	end_if(),
	?assertMatch([[{'if',_}, [{ok,"foo"}], elseif, [{ok,"bar"}]]], get_stmt()).

expr2_test() ->
	init(),
	start_let({name,0,"a"}, {expr, 123}),
	text("foo"),
	start_foreach({name,0,"b"}, {expr, 456}),
	text("hoge"),
	else(),
	text("moge"),
	end_foreach(),
	text("hogehoge"),
	else(),
	text("bar"),
	end_let(),
	?assertMatch([[{'let',_,_}, [{ok,"foo"}, {foreach,_,_}, [{ok,"hoge"}], elseforeach, [{ok,"moge"}], {ok,"hogehoge"}], elselet, [{ok,"bar"}]]], get_stmt()).

expr3_test() ->
	init(),
	start_if({expr, true}),
	else(),
	?assertThrow({{templ_parse_handler,einval}, 
				  [[],{elseif,[{'if',{expr,true}},[]]}], "wrong syntax"}, 
				 end_let()),
	end_if(),
	?assertMatch([[{'if',_}, [], elseif, []]], get_stmt()).

expr4_test() ->
	init(),
	start_if({expr, true}),
	start_let({name,0,"a"}, {expr, 1}),
	?assertThrow({{templ_parse_handler,einval},
				  [[],{{'let',{name,0,"a"},{expr,1}},[]}], "wrong syntax"},
				 end_if()),
	end_let(),
	end_if(),
	?assertMatch([[{'if',_}, [{'let',_,_}, []]]], get_stmt()).
